package com.yjs.app.trade.service.service.util;

import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.yjs.app.trade.core.util.DateUtil;
import com.yjs.app.trade.core.util.PropertiesUtils;

@Component
public class TaskService implements ApplicationListener<ContextRefreshedEvent> {

	@Autowired
	RabbitMQService rabbitMQService;
	@Autowired
	KafkaService kafkaService;
	
	private Logger log = LoggerFactory.getLogger(Logger.class);
	
	private ScheduledExecutorService scheduledExecutorService;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if (event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")) {
			this.taskModel();
		} else {
			log.info("-----------");
		}
	}
	
	/**
	 * 启动任务
	 */
	private void taskModel () {
		this.rabbitMQTask();
//		this.kafkaTask();
		this.timeTask();
	}
	
	/**
	 * rabbitMQ队列消费端
	 */
	private void rabbitMQTask () {
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					rabbitMQService.routingRecv(PropertiesUtils.getProperty("rabbitmq.url").toString(), 
							Integer.parseInt(PropertiesUtils.getProperty("rabbitmq.port").toString()), 
							PropertiesUtils.getProperty("rabbitmq.username").toString(), 
							PropertiesUtils.getProperty("rabbitmq.password").toString(), 
							PropertiesUtils.getProperty("rabbitmq.queueName").toString(),
							PropertiesUtils.getProperty("rabbitmq.durable").toString(),
							PropertiesUtils.getProperty("rabbitmq.exchangeName").toString(),
							PropertiesUtils.getProperty("rabbitmq.routingKey").toString());
					log.info("rabbitMq消费端已启动-----------");
				} catch (Exception e) {
					e.printStackTrace();
					log.error("rabbitMq消费端启动报错~~~~~~" + e);
				}
			}
		});
		thread.start();
	}
	
	/**
	 * rabbitMQ队列消费端
	 */
	@SuppressWarnings("unused")
	private void kafkaTask () {
		Thread orderThread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					kafkaService.kafkaConsumer(PropertiesUtils.getProperty("kafka.bootstrapServers").toString(), 
							PropertiesUtils.getProperty("kafka.order.groupId").toString(),
							PropertiesUtils.getProperty("kafka.order.topic").toString());
					log.info("kafka订单消费端已启动-----------");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		orderThread.start();
		Thread contractThread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					kafkaService.kafkaConsumer(PropertiesUtils.getProperty("kafka.bootstrapServers").toString(), 
							PropertiesUtils.getProperty("kafka.contract.groupId").toString(),
							PropertiesUtils.getProperty("kafka.contract.topic").toString());
					log.info("kafka合同消费端已启动-----------");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		contractThread.start();
	}
	
	/**
	 * 定时任务
	 */
	public void timeTask () {
		long oneDay = 24 * 60 * 60 * 1000;
		long intTime = 0l;
		try {
			intTime = DateUtil.dateFormat.parse(DateUtil.dateFormat.format(new Date()) + " 01:00:00").getTime() - System.currentTimeMillis();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		intTime = intTime > 0 ? intTime : oneDay + intTime;
		scheduledExecutorService = Executors.newScheduledThreadPool(1);
		log.info("定时任务已启动-----------" + intTime);
		scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				log.info("定时任务已执行-----------" + new Date());
			}
		}, intTime, oneDay, TimeUnit.MILLISECONDS);
	}
	
	public static void main(String[] args) {
		long oneDay = 24 * 60 * 60 * 1000;
		long intTime = 0l;
		try {
			intTime = DateUtil.dateFormat.parse(DateUtil.dateFormat.format(new Date()) + " 01:00:00").getTime() - System.currentTimeMillis();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		intTime = intTime > 0 ? intTime : oneDay + intTime;
		System.out.println(intTime / 1000 / 60);
	}
}	
